var app = getApp();
var Util= require('../../utils/util.js')
import Dialog from '../../vant/dialog/dialog';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '验票',
      'color': false,
    },
    time: '',
    date: '',
    week: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId,
  });
  },
  catchTouchMove: function (res) {
    return false
  },
  onColse:function(){
    this.setData({ window: false});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  updateTime:function () {
    var cd = new Date();
    this.setData({
      time: this.zeroPadding(cd.getHours(), 2) + ':' + this.zeroPadding(cd.getMinutes(), 2) + ':' + this.zeroPadding(cd.getSeconds(), 2),
      date: this.zeroPadding(cd.getFullYear(), 4) + '-' + this.zeroPadding(cd.getMonth() + 1, 2) + '-' + this.zeroPadding(cd.getDate(), 2) + ' ' + this.data.week[cd.getDay()]
    })
  },

  zeroPadding: function(num, digit) {
    var zero = '';
    for (var i = 0; i < digit; i++) {
        zero += '0';
    }
    return (zero + num).slice(-digit);
 },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    var timerID = setInterval(function () {
      that.updateTime()
      }, 1000);
    that.updateTime();

    if(app.globalData.isLog && app.globalData.token) this.vail_ticket();
  },
  vail_ticket:function(){
    var that = this;
    app.baseGet(app.U({ c: 'user_api', a:'vail_ticket', q: { uni:that.data.orderId}}),function(res){
      that.setData({
        isOvertime: res.data.is_overtime,
        isUse: res.data.is_use,
        totalNum: res.data.total_num,
        ticketInfo: res.data
    });
    });
  },

  guoqi: function(){
    wx.showToast({
      title: '票已过期，请勿检票！',
      icon: 'none',
      duration: 1000,
      mask: true,
    });
  },
  yanp: function(){
    let that=this
    Dialog.confirm({
        title: '确认检票',
        message: '确认验票后，默认该乘客已乘车，将不可取消订单，请于乘车时进行检票上车！',
        confirmButtonText: '确认检票',
        cancelButtonText: '暂不检票'
    }).then(() => {
      that.onfirm()
     }).catch(() => {
      wx.showToast({
        title: '暂不检票',
        icon: 'none',
        duration: 1000,
        mask: true,
      });
    });
  },
  onfirm(){
    let that=this
    setTimeout(function () {
        app.baseGet(app.U({ c: 'user_api', a:'check_piao', q: { order_id:that.data.orderId}}),function(res){
          console.log(res)
          if(res.code==200){
            wx.showToast({
              title: '检票成功',
              icon: 'success',
              duration: 1000,
              mask: true,
            });
            that.setData({
              isUse: 1
            })
        }else {
          wx.showToast({
            title: '检票失败',
            icon: 'none',
            duration: 1000,
            mask: true,
          });
        }
        });

    },500)
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ window:false});
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getIndexConfig();
    if (app.globalData.isLog && app.globalData.token) this.get_issue_coupon_list();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})